//
//  RVMyViewController.m
//  RememberVocabularies
//
//  Created by 伍银荣 on 2017/10/14.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "RVMyViewController.h"
#import "RVLoginViewController.h"
#import "SatisfactionViewController.h"
#import "RVRegardViewController.h"
#import "RVMyCollectedViewController.h"
#import "SVProgressHUD.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>


@interface RVMyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UILabel *loginLabel;
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic,strong) NSArray *imgArr;
@property (nonatomic,strong) NSArray *arr;

@end

@implementation RVMyViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _loginLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"phoneNum"];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.rowHeight = 60;
    
    _myTableView.tableFooterView = [UIView new];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注销" style:UIBarButtonItemStyleDone target:self action:@selector(logoutClick)];
    
    _arr = @[@"我的词库",@"满意度调查",@"清空缓存",@"分享有礼",@"关于我们"];
    
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        NSString *imgStr = [NSString stringWithFormat:@"me%d",i + 1];
        [marr addObject:imgStr];
    }
    self.imgArr = marr;
}

- (IBAction)LoginBtn:(id)sender {
    if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"phoneNum"] isEqualToString:@"请登录"]) {
        return;
    }else{
        RVLoginViewController *loginVC = [[RVLoginViewController alloc]init];
        loginVC.navigationItem.title = @"登录";
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.imageView.image = [UIImage imageNamed:_imgArr[indexPath.row]];
    cell.textLabel.text = _arr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        SatisfactionViewController *satisfactionVC = [[SatisfactionViewController alloc]init];
        [self.navigationController pushViewController:satisfactionVC animated:YES];
    }else if (indexPath.row == 4){
        RVRegardViewController *regardVC = [[RVRegardViewController alloc]init];
        [self.navigationController pushViewController:regardVC animated:YES];
    }else if (indexPath.row == 2){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清空缓存" message:@"确认删除所有缓存文件吗？" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [@[] writeToFile:collectedPlist atomically:YES];
            [@[] writeToFile:finishPlist atomically:YES];
            [SVProgressHUD showSuccessWithStatus:@"清除成功"];
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (indexPath.row == 3){
        
        //1、创建分享参数
        NSArray* imageArray = @[[UIImage imageNamed:@"icon.png"]];
        if (imageArray) {
            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            NSString *paramsText = @" 著名语言学家Greg Thomson说的：“外语学习的原理是如此之复杂，以至于没有人能说清楚；但掌握语言的过程又是如此之简单，以至于不需要说清楚。”讨论语言学习的原理，是件非常复杂的事，在下当然也没把握能描述得很清楚。";
            [shareParams SSDKSetupShareParamsByText:paramsText
            images:imageArray url:[NSURL URLWithString:@"http://mob.com"]title:@"分享标题" type:SSDKContentTypeAuto];
            //有的平台要客户端分享需要加此方法，例如微博
            [shareParams SSDKEnableUseClientShare];
            //2、分享（可以弹出我们的分享菜单和编辑界面）
            //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图，只有传这个才可以弹出我们的分享菜单，可以传分享的按钮对象或者自己创建小的view 对象，iPhone可以传nil不会影响
            [ShareSDK showShareActionSheet:nil items:nil shareParams:shareParams
                       onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                           switch (state) {
                               case SSDKResponseStateSuccess:
                               {
                                   UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                    message:nil delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil];
                                   [alertView show];
                                   break;
                               }
                               case SSDKResponseStateFail:
                               {
                                   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"%@",error]delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
                                   [alert show];
                                   break;
                               }
                               default:
                                   break;
                           }
                       }
             ];}
    } else {
        [self.navigationController pushViewController:[RVMyCollectedViewController new] animated:YES];
    }
    [_myTableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

//注销按钮的点击事件
-(void)logoutClick
{
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"phoneNum"] isEqualToString:@"请登录"]) {
        [self alertTitle:@"温馨提示" alertMessage:@"您还没有登录哦"];
    }else{
        [self.navigationController pushViewController:[RVLoginViewController new] animated:YES];
        
     [[NSUserDefaults standardUserDefaults] setObject:@"请登录" forKey:@"phoneNum"];
    }
}

-(void)alertTitle:(NSString *)title alertMessage:(NSString *)message
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
